package com.ksd.web.controller.tool;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import javax.servlet.http.HttpServletRequest;

import com.ksd.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class IpConfigTool {
    private static Logger logger = LoggerFactory.getLogger(IpConfigTool.class);

    public static LinuxNetwork getLinuxIpConfigByNetworkPath(File file) {
        try {
            ///etc/sysconfig/network-scripts/ifconfg-th0xxxxx
            BufferedReader	reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            LinuxNetwork network = new LinuxNetwork();
            while ((tempString = reader.readLine()) != null) {
                if (tempString.startsWith("IPADDR")) {
                    String ip = "IPADDR=";
                    ip = tempString.substring(ip.length());
                    System.out.println("获取服务器IP："+ip);
                    network.setIpAddress(ip);
                } else if (tempString.startsWith("NETMASK")) {
                    String netmask = "NETMASK=";
                    netmask = tempString.substring(netmask.length());
                    network.setNetmask(netmask);
                    System.out.println("子网掩码："+netmask);
                } else if (tempString.startsWith("GATEWAY")) {
                    String gateway = "GATEWAY=";
                    gateway = tempString.substring(gateway.length());
                    network.setGateway(gateway);
                    System.out.println("网关："+gateway);
                } else if (tempString.startsWith("MAC")) {
                    String mac = "MAC=";
                    mac = tempString.substring(mac.length());
                    network.setMacAddress(mac);
                    System.out.println("MAC地址:"+mac);
                }
            }
            reader.close();
            return network;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }


    public static LinuxNetwork getLinuxIpConfig(String path) {
        try {
            ///etc/sysconfig/network-scripts/ifconfg-th0xxxxx
//            File file = new File(UDRMConfig.getNetworkPath());
            File file = new File(path);
            if (!file.exists()) {
                LinuxNetwork linuxNetwork = new LinuxNetwork();
                linuxNetwork.setIpAddress("");
                linuxNetwork.setGateway("");
                linuxNetwork.setNetmask("");
                linuxNetwork.setMacAddress("");
                return linuxNetwork;
            }
            BufferedReader	reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            LinuxNetwork network = new LinuxNetwork();
            while ((tempString = reader.readLine()) != null) {
                if (tempString.startsWith("IPADDR")) {
                    String ip = "IPADDR=";
                    ip = tempString.substring(ip.length());
                    System.out.println("获取服务器IP："+ip);
                    network.setIpAddress(ip);
                } else if (tempString.startsWith("NETMASK")) {
                    String netmask = "NETMASK=";
                    netmask = tempString.substring(netmask.length());
                    network.setNetmask(netmask);
                    System.out.println("子网掩码："+netmask);
                } else if (tempString.startsWith("GATEWAY")) {
                    String gateway = "GATEWAY=";
                    gateway = tempString.substring(gateway.length());
                    network.setGateway(gateway);
                    System.out.println("网关："+gateway);
                }else if (tempString.startsWith("MAC")) {
                    String mac = "MAC=";
                    mac = tempString.substring(mac.length());
                    network.setMacAddress(mac);
                    System.out.println("MAC地址:"+mac);
                }
            }
            reader.close();
            return network;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }


    /***
     * 获取客户端ip地址
     * @param request
     */
    public static String getIP(final HttpServletRequest request) throws Exception {
        if (request == null) {
            throw (new Exception("getIpAddr method HttpServletRequest Object is null"));
        }
        String ipStr = request.getHeader("x-forwarded-for");
        if (StringUtils.isBlank(ipStr) || "unknown".equalsIgnoreCase(ipStr)) {
            ipStr = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isBlank(ipStr) || "unknown".equalsIgnoreCase(ipStr)) {
            ipStr = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isBlank(ipStr) || "unknown".equalsIgnoreCase(ipStr)) {
            ipStr = request.getRemoteAddr();
        }

        // 多个路由时，取第一个非unknown的ip
        final String[] arr = ipStr.split(",");
        for (final String str : arr) {
            if (!"unknown".equalsIgnoreCase(str)) {
                ipStr = str;
                break;
            }
        }
        //目的是将localhost访问对应的ip 0:0:0:0:0:0:0:1 转成 127.0.0.1。
        return ipStr.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ipStr;
    }
}
